home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / csrc1.arc / CRYPT.C < prev    next >
C/C++ Source or Header  |  1989-07-27  |  2KB  |  104 lines

  1. /*
  2.  * duplicate crypt.s in a readable language
  3.  */
  4.  
  5. /*)BUILD
  6. */
  7.  
  8. static int wheeldiv[] {
  9.     32,18,10,6,4
  10. };
  11. static int shift[] {
  12.     0000001,0000002,0000004,0000010,0000020,0000040,
  13.     0000100,0000200,0000400,0001000,0002000,0004000,
  14.     0010000,0020000,0040000,0100000,0000001,0000002
  15. };
  16. static char word[32];
  17.  
  18. char *
  19. crypt(ukey)
  20. char *ukey;          /* pointer to user's string (max 63 chars)    */
  21. {      
  22.     char *s, *k;
  23.     int *w, *d, *c, r, t;
  24.  
  25.     char key[128];
  26.     int wheelcode[128],cagecode[128],cage[128],wheel[128];
  27.  
  28.     /*
  29.      * copy the users key into new space and add junk if necessary
  30.      */
  31.     k = key;  s = ukey;
  32.     *k++ = 004;  *k++ = 034;
  33.     while ((k < &key[64]) && (*k++ = *s++)) ;
  34.     --k; s = key;
  35.     while (k < &key[128])  *k++ = k[-1] ^ *s++;
  36.  
  37.     /*
  38.      * establish wheel codes and cage codes
  39.      */
  40.     w = wheelcode; c = cagecode;  t = 256;
  41.     while (w < &wheelcode[128])
  42.     {       r = 0;  *w = 0;  d = wheeldiv;
  43.         *c = 0;
  44.         while (d < &wheeldiv[5])
  45.         {       r = (r + (t % *d++)) & ~040;
  46.             *w |= shift[r/2];
  47.             if (d < &wheeldiv[3]) 
  48.                 *c |= shift[r/2+2];
  49.         }
  50.         w++;  c++;  t -= 2;
  51.     }
  52.     /*
  53.      * make the internal settings of the machine.
  54.      * both the lugs on the 128 cage bars and the lugs
  55.      * on the 16 wheels are set from the expanded key
  56.      */
  57.  
  58.     k = key;  c = cage;  w = wheel;
  59.     while (k < &key[128])
  60.     {       t = *k++ & 0177;
  61.         *c++ = cagecode[t];
  62.         *w++ = wheelcode[t];
  63.     }
  64.  
  65.     /*
  66.      * now spin the cage against the wheel to produce output
  67.      */
  68.  
  69.     k = word;  w = &wheel[64];
  70.     while (k < &word[8]) {
  71.         c = cage;  r = 0;  t = *--w;
  72.         while (c < &cage[128]) {
  73.             if ((*c++ & t))
  74.                 r++;
  75.         }
  76.         /*
  77.          * we now have a piece of output from current wheel
  78.          * it needs to be folded to remove lingering hopes of
  79.          * inverting the function
  80.          */
  81.         r = r % 62 + '0';
  82.         if (r > '9') {
  83.             r += ('A'-'9'-1);
  84.             if (r > 'Z')
  85.                 r += ('a'-'Z'-1);
  86.         }
  87.         *k++ = r;
  88.     }
  89.  
  90.     return(word);
  91. }
  92.  
  93. main()          /* special purpose routine: test on one password */
  94. {
  95.     register char c, *s;
  96.     char str[20];
  97.     for (;;)
  98.     {       printf("password: ");
  99.         for (s=str; (c=getchar()) != '\n';) *s++ = c;
  100.         *s = 0;
  101.         printf("crypt(%s)=%s\n",str,crypt(str));
  102.     }
  103. }
  104.